package show.me.the.money.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.*;

import show.me.the.money.resource.BinaryFile;
import show.me.the.money.resource.ResourceFile;




public class HTMLTemplateLoader extends HttpServlet implements Servlet {;
	private static final long serialVersionUID = -8260076694457868118L;
	private static ResourceFile templateFile;
	private static Log logger = LogFactory.getLog(HTMLTemplateLoader.class);
	
	public void init(ServletConfig oCfg) throws ServletException {
		String cfgPath = oCfg.getInitParameter("templatePath");
		String templatePath = oCfg.getServletContext().getRealPath(cfgPath);
		templateFile = new BinaryFile();
		templateFile.setFileName(templatePath);
		try {
			templateFile.load();
		} catch (IOException e) {
			logger.error("HTMLTemplate File load Failed:", e);
		}
		logger.info("inited..");
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response){
		try {
			if(templateFile.exists()){
				//response.addHeader("content-type", contentType);
				templateFile.writeToStream(response.getOutputStream());
			}
			else{
				response.setStatus(404);
				logger.fatal("HTMLTemplate file not found.");
			}
		} 
		catch (Exception e) {
			response.setStatus(500);
			logger.fatal(e);
		}
		
	}
	

	
}